<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\PageModel;

class Page extends AdminBase
{
    public function index()
    {
        $list = (new PageModel)->getTreeList();
        return view('',['list'=>$list])->filter(function($content){
            return str_replace("&amp;emsp;",' ',$content);
        });
    }

    /**
     * 编辑单页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = PageModel::update($param);
            if( $result ) {
                xn_add_admin_log('编辑单页');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = PageModel::where('id',$id)->find();
        $list = (new PageModel)->getTreeList();
        return view('form',['data'=>$data,'list'=>$list,'pid'=>$data['pid']])->filter(function($content){
            return str_replace("&amp;emsp;",' ',$content);
        });
    }

    /**
     * 添加单页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = PageModel::create($param);
            if( $result ) {
                xn_add_admin_log('添加单页');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $list = (new PageModel)->getTreeList();
        return view('form',['list'=>$list,'pid'=>$this->request->get('pid')])->filter(function($content){
            return str_replace("&amp;emsp;",' ',$content);
        });
    }

    /**
     * 删除单页
     */
    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        $child_count = PageModel::where('pid',$id)->count();
        $child_count && $this->error('请先删除子节点');
        PageModel::destroy($id);
        xn_add_admin_log('删除单页');
        $this->success('删除成功');
    }

    /**
     * 排序
     */
    public function sort()
    {
        $param = $this->request->post();
        foreach ($param as $k => $v) {
            $v=empty($v) ? null : $v;
            PageModel::where('id', $k)->save(['sort'=>$v]);
        }
        xn_add_admin_log('排序单页');
        $this->success('排序成功', url('index'));
    }
}
